package com.like.entity;

import android.app.Activity;
import android.app.DialogFragment;
import android.app.Fragment;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.like.dialog.LoadingDialogFragment;
import com.like.likeutils.storage.SPUtils;

public class M extends BaseM {

    public static final String LOGINED_USER = "logined_user";

    private LoadingDialogFragment mLoadingDialog;

    public M(Activity activity) {
        super(activity);
        init();
    }

    public M(Fragment fragment) {
        super(fragment);
        init();
    }

    private void init() {
        mLoadingDialog = new LoadingDialogFragment();
        setLoadingDialog(mLoadingDialog);
    }

    public LoginedUser getLoginedUser() {
        String loginedUserJson = (String) SPUtils.get(mContext, LOGINED_USER, "");
        if(TextUtils.isEmpty(loginedUserJson))
            return null;
        LoginedUser user = new Gson().fromJson(loginedUserJson, LoginedUser.class);
        return user;
    }

    public void setLoginedUser(LoginedUser user) {
        String json = new Gson().toJson(user);
        SPUtils.put(mContext, LOGINED_USER, json);
    }

    public void logout() {
        SPUtils.remove(mContext, LOGINED_USER);
    }

}
